package com.ryhx.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.ryhx.util.ConfigUtil;
/**
 * Dao  和   Service 以及实现类文件生成器
 * @author yaojinshan
 * @since 2017-07-19 13:01
 */
public class DaoAndServiceProducer {
	static FileWriter fw;
	
	public static void main(String[] args) {
		createDaoAndServiceAndImplFile();
	}
	/**
	 * 创建dao ,daoImpl,service,serviceImpl 文件
	 */
	public static void createDaoAndServiceAndImplFile() {
		FileDirProducer.createDir();
		createModelDao();
		createModelDaoImpl();
		createModelService();
		createModelServiceImpl();
	}
	
	/**
	 * 创建Dao文件
	 */
	private static void createModelDao() {
		System.out.println("---生产Dao文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---start");
		try {
			// 按行读取DaoTemplate的内容
			String daoTemplateFileSourceString = readFileByLines("template/DaoTemplate.txt");
			daoTemplateFileSourceString = daoTemplateFileSourceString.replace("package ",
					"package " + ConfigUtil.daoPackagePath + "." + ConfigUtil.simpleModelNameLower + ".dao"); // 替换第一行的包名
			daoTemplateFileSourceString = daoTemplateFileSourceString.replace("import Model", "import " + ConfigUtil.modelPackagePath + "." + ConfigUtil.simpleModelName);// 替换model引入内容
			daoTemplateFileSourceString = daoTemplateFileSourceString.replace("import Exception",
					"import " + ConfigUtil.excetpionName);// 替换exception引入内容
			daoTemplateFileSourceString = daoTemplateFileSourceString.replaceAll("TemplateDao",
					ConfigUtil.simpleModelName + "Dao");// 替换掉接口名称
			daoTemplateFileSourceString = daoTemplateFileSourceString.replaceAll("Template", ConfigUtil.simpleModelName);// 替换掉所有类名称
			daoTemplateFileSourceString = daoTemplateFileSourceString.replaceAll("template",
					ConfigUtil.firstCharLowerSimpleModelName);// 替换掉所有实体实例名称
			daoTemplateFileSourceString = daoTemplateFileSourceString.replaceAll("CustomerException",
					ConfigUtil.simpleExceptionName);// 替换掉所有的异常名称

			File daoFile = new File("source/" + ConfigUtil.simpleModelNameLower + "/dao/" + ConfigUtil.simpleModelName + "Dao.java");
			if (!daoFile.exists()) {
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Dao文件:" + daoFile.getName() + "创建");
				daoFile.createNewFile();
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Dao文件:" + daoFile.getName() + "创建完成");
			}
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Dao文件:" + daoFile.getName() + "写入");
			fw = new FileWriter(daoFile);
			fw.write(daoTemplateFileSourceString);
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Dao文件:" + daoFile.getName() + "写入完成");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("---生产Dao文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---end");
		System.out.println();
	}
	/**
	 * 创建DaoImpl文件
	 */
	private static void createModelDaoImpl() {
		System.out.println("---生产DaoImpl文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---start");
		try {
			// 按行读取DaoImplTemplate的内容
			String daoImplTemplateFileSourceString = readFileByLines("template/DaoImplTemplate.txt");
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replace("package ","package " + ConfigUtil.daoPackagePath + "." + ConfigUtil.simpleModelNameLower + ".dao.impl"); // 替换第一行的包名
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replace("import Model", "import " + ConfigUtil.modelPackagePath + "." + ConfigUtil.simpleModelName);// 替换model引入内容
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replace("import DaoModel", "import " + ConfigUtil.daoPackagePath + "." + ConfigUtil.simpleModelNameLower + ".dao." + ConfigUtil.simpleModelName + "Dao");// 替换modelDao引入内容
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replace("import Exception",
					"import " + ConfigUtil.excetpionName);// 替换exception引入内容
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replaceAll("templateDao",
					ConfigUtil.firstCharLowerSimpleModelName + "Dao");// 替换注解Dao名称
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replaceAll("TemplateModelDaoImpl",
					ConfigUtil.simpleModelName + "DaoImpl");// 替换掉接口名称
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replaceAll("TemplateModelDao",
					ConfigUtil.simpleModelName + "Dao");// 替换掉实现的接口名称
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replaceAll("TemplateModel", ConfigUtil.simpleModelName);// 替换掉所有类名称
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replaceAll("templateModel",
					ConfigUtil.firstCharLowerSimpleModelName);// 替换掉所有实体实例名称
			daoImplTemplateFileSourceString = daoImplTemplateFileSourceString.replaceAll("CustomerException",
					ConfigUtil.simpleExceptionName);// 替换掉所有的异常名称
			
			File daoFile = new File("source/" + ConfigUtil.simpleModelNameLower + "/dao/impl/" + ConfigUtil.simpleModelName + "DaoImpl.java");
			if (!daoFile.exists()) {
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的DaoImpl文件:" + daoFile.getName() + "创建");
				daoFile.createNewFile();
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的DaoImpl文件:" + daoFile.getName() + "创建完成");
			}
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的DaoImpl文件:" + daoFile.getName() + "写入");
			fw = new FileWriter(daoFile);
			fw.write(daoImplTemplateFileSourceString);
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的DaoImpl文件:" + daoFile.getName() + "写入完成");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("---生产DaoImpl文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---end");
		System.out.println();
	}
	/**
	 * 创建Service文件
	 */
	private static void createModelService() {
		System.out.println("---生产Service文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---start");
		try {
			// 按行读取ServiceTemplate的内容
			String serviceTemplateFileSourceString = readFileByLines("template/ServiceTemplate.txt");
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replace("package ",
					"package " + ConfigUtil.servicePackagePath  + "." + ConfigUtil.simpleModelNameLower + ".service"); // 替换第一行的包名
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replace("import Model", "import " + ConfigUtil.modelPackagePath + "." + ConfigUtil.simpleModelName);// 替换model引入内容
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replace("import Exception",
					"import " + ConfigUtil.excetpionName);// 替换exception引入内容
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replaceAll("TemplateService",
					ConfigUtil.simpleModelName + "Service");// 替换掉接口名称
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replaceAll("Template", ConfigUtil.simpleModelName);// 替换掉所有类名称
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replaceAll("template",
					ConfigUtil.firstCharLowerSimpleModelName);//替换掉实例名称
			serviceTemplateFileSourceString = serviceTemplateFileSourceString.replaceAll("CustomerException",
					ConfigUtil.simpleExceptionName);// 替换掉所有的异常名称
			
			File serviceFile = new File("source/" + ConfigUtil.simpleModelNameLower + "/service/" + ConfigUtil.simpleModelName + "Service.java");
			if (!serviceFile.exists()) {
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Service文件:" + serviceFile.getName() + "创建");
				serviceFile.createNewFile();
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Service文件:" + serviceFile.getName() + "创建完成");
			}
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Service文件:" + serviceFile.getName() + "写入");
			fw = new FileWriter(serviceFile);
			fw.write(serviceTemplateFileSourceString);
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的Service文件:" + serviceFile.getName() + "写入完成");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("---生产Service文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---end");
		System.out.println();
	}
	/**
	 * 创建ServiceImpl文件
	 */
	private static void createModelServiceImpl() {
		System.out.println("---生产ServiceImpl文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---start");
		try {
			// 按行读取ServiceImplTemplate的内容
			String serviceImplTemplateFileSourceString = readFileByLines("template/ServiceImplTemplate.txt");
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replace("package ","package " + ConfigUtil.servicePackagePath + "." + ConfigUtil.simpleModelNameLower + ".service.impl"); // 替换第一行的包名
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replace("import Model", "import " + ConfigUtil.modelPackagePath + "." + ConfigUtil.simpleModelName);// 替换model引入内容
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replace("import DaoModel", "import " + ConfigUtil.daoPackagePath + "." + ConfigUtil.simpleModelNameLower + ".dao." + ConfigUtil.simpleModelName + "Dao");// 替换DaoModel引入内容
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replace("import ServiceModel", "import " + ConfigUtil.servicePackagePath + "." + ConfigUtil.simpleModelNameLower + ".service." + ConfigUtil.simpleModelName + "Service");// 替换ServiceModel引入内容
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replace("import Exception","import " + ConfigUtil.excetpionName);// 替换exception引入内容
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("templateService",ConfigUtil.firstCharLowerSimpleModelName + "Service");// 替换注解service名称
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("TemplateServiceImpl",ConfigUtil.simpleModelName + "ServiceImpl");// 替换掉类名称
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("TemplateService",ConfigUtil.simpleModelName + "Service");// 替换掉实现的接口名称
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("templateDao", ConfigUtil.firstCharLowerSimpleModelName + "Dao");// 替换掉 dao实例名称
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("TemplateDao",ConfigUtil.simpleModelName + "Dao");// 替换掉所有Dao类名
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("Template",ConfigUtil.simpleModelName);// 替换掉所有类名
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("template",ConfigUtil.firstCharLowerSimpleModelName);// 替换掉所有类实例名称
			serviceImplTemplateFileSourceString = serviceImplTemplateFileSourceString.replaceAll("CustomerException",ConfigUtil.simpleExceptionName);// 替换掉所有的异常名称
			
			File serviceFile = new File("source/" + ConfigUtil.simpleModelNameLower + "/service/impl/" + ConfigUtil.simpleModelName + "ServiceImpl.java");
			if (!serviceFile.exists()) {
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的ServiceImpl文件:" + serviceFile.getName() + "创建");
				serviceFile.createNewFile();
				System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的ServiceImpl文件:" + serviceFile.getName() + "创建完成");
			}
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的ServiceImpl文件:" + serviceFile.getName() + "写入");
			fw = new FileWriter(serviceFile);
			fw.write(serviceImplTemplateFileSourceString);
			System.out.println("---初始化实体简称：" + ConfigUtil.simpleModelName + "的ServiceImpl文件:" + serviceFile.getName() + "写入完成");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("---生产serviceImpl文件---实体全路径名称：" + ConfigUtil.modelPackagePath + ConfigUtil.simpleModelName + ",实体简称：" + ConfigUtil.simpleModelName + "---end");
		System.out.println();
	}
	/**
	 * 按行读取文件
	 * @param fileName
	 * @return
	 */
	public static String readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			String returnStr = "";
			//int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				//System.out.println("line " + line + ": " + tempString);
				returnStr += tempString + "\n";
				//line++;
			}
			reader.close();
			return returnStr;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return "";
	}
}
